{% extends 'base.html' %}
{% load static %}

{% block title %}系统日志 - 固定资产管理系统{% endblock %}

{% block extra_css %}
<style>
    .log-level-DEBUG { color: #6c757d; }
    .log-level-INFO { color: #17a2b8; }
    .log-level-WARNING { color: #ffc107; }
    .log-level-ERROR { color: #dc3545; }
    .log-level-CRITICAL { color: #dc3545; font-weight: bold; }
    
    .stats-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
    }
    
    .filter-card {
        background: #f8f9fa;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
    }
    
    .log-table {
        background: white;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-list-alt"></i> 系统日志</h2>
                <div>
                    <button class="btn btn-outline-secondary" onclick="location.reload()">
                        <i class="fas fa-sync-alt"></i> 刷新
                    </button>
                </div>
            </div>
            
            <!-- 统计信息 -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-list"></i> 总日志数</h5>
                        <h3>{{ stats.total }}</h3>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-exclamation-triangle"></i> 错误日志</h5>
                        <h3>{{ stats.error_count }}</h3>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-exclamation-circle"></i> 警告日志</h5>
                        <h3>{{ stats.warning_count }}</h3>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-calendar-day"></i> 今日日志</h5>
                        <h3>{{ stats.today_count }}</h3>
                    </div>
                </div>
            </div>
            
            <!-- 筛选条件 -->
            <div class="filter-card">
                <form method="get" class="row g-3">
                    <div class="col-md-2">
                        <label class="form-label">日志级别</label>
                        <select name="level" class="form-select">
                            <option value="">全部</option>
                            {% for value, label in level_choices %}
                                <option value="{{ value }}" {% if level_filter == value %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">模块</label>
                        <select name="module" class="form-select">
                            <option value="">全部</option>
                            {% for value, label in module_choices %}
                                <option value="{{ value }}" {% if module_filter == value %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">开始日期</label>
                        <input type="date" name="date_from" class="form-control" value="{{ date_from }}">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">结束日期</label>
                        <input type="date" name="date_to" class="form-control" value="{{ date_to }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">搜索</label>
                        <input type="text" name="search" class="form-control" placeholder="搜索日志内容..." value="{{ search_query }}">
                    </div>
                    <div class="col-md-1">
                        <label class="form-label">&nbsp;</label>
                        <button type="submit" class="btn btn-primary d-block">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>
                </form>
            </div>
            
            <!-- 日志列表 -->
            <div class="log-table">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-dark">
                            <tr>
                                <th>时间</th>
                                <th>级别</th>
                                <th>模块</th>
                                <th>操作</th>
                                <th>用户</th>
                                <th>IP地址</th>
                                <th>消息</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in page_obj %}
                            <tr>
                                <td>
                                    <small>{{ log.created_at|date:"Y-m-d H:i:s" }}</small>
                                </td>
                                <td>
                                    <span class="badge log-level-{{ log.level }}">
                                        {{ log.get_level_display }}
                                    </span>
                                </td>
                                <td>
                                    <span class="badge bg-secondary">{{ log.get_module_display }}</span>
                                </td>
                                <td>{{ log.action }}</td>
                                <td>
                                    {% if log.user %}
                                        <i class="fas fa-user"></i> {{ log.user.username }}
                                    {% else %}
                                        <span class="text-muted">系统</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.ip_address %}
                                        <code>{{ log.ip_address }}</code>
                                    {% else %}
                                        <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="text-truncate" style="max-width: 300px;" title="{{ log.message }}">
                                        {{ log.message }}
                                    </div>
                                    {% if log.details %}
                                        <button class="btn btn-sm btn-outline-info mt-1" type="button" data-bs-toggle="collapse" data-bs-target="#details-{{ log.id }}">
                                            <i class="fas fa-info-circle"></i> 详情
                                        </button>
                                        <div class="collapse mt-2" id="details-{{ log.id }}">
                                            <div class="card card-body">
                                                <pre class="mb-0"><code>{{ log.details|pprint }}</code></pre>
                                            </div>
                                        </div>
                                    {% endif %}
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="7" class="text-center text-muted py-4">
                                    <i class="fas fa-inbox fa-3x mb-3"></i>
                                    <p>暂无日志记录</p>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                <!-- 分页 -->
                {% if page_obj.has_other_pages %}
                <div class="d-flex justify-content-between align-items-center p-3 border-top">
                    <div class="text-muted">
                        显示第 {{ page_obj.start_index }} - {{ page_obj.end_index }} 条，共 {{ page_obj.paginator.count }} 条
                    </div>
                    <nav>
                        <ul class="pagination mb-0">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.previous_page_number }}">上一页</a>
                                </li>
                            {% endif %}
                            
                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ num }}</span>
                                    </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ num }}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            
                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.next_page_number }}">下一页</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 自动刷新功能
    let autoRefresh = false;
    let refreshInterval;
    
    $('#auto-refresh').change(function() {
        autoRefresh = $(this).is(':checked');
        if (autoRefresh) {
            refreshInterval = setInterval(function() {
                location.reload();
            }, 30000); // 30秒刷新一次
        } else {
            clearInterval(refreshInterval);
        }
    });
});
</script>
{% endblock %}